package com.infoservice.dms.chana.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import com.infodms.dms.common.Constant;
import com.infoservice.dms.chana.common.DEConstant;
import com.infoservice.dms.chana.common.DEUtil;
import com.infoservice.dms.chana.common.RpcException;
import com.infoservice.dms.chana.vo.BaseVO;
import com.infoservice.dms.chana.vo.VehicleShippingDetailVO;
import com.infoservice.dms.chana.vo.VehicleShippingVO;

/**
 * @Title: DeVehicleShippingDao.java
 *
 * @Description:CHANADMS
 *
 * @Copyright: Copyright (c) 2010
 *
 * @Company: www.infoservice.com.cn
 * @Date: 2010-7-26
 *
 * @author lishuai 
 * @mail   lishuai103@yahoo.cn	
 * @version 1.0
 * @remark 
 */
public class DeVehicleShippingDao extends AbstractIFDao implements DEConstant {
	public static Logger logger = Logger.getLogger(DeVehicleShippingDao.class);
	private static final DeVehicleShippingDao dao = new DeVehicleShippingDao ();
	private static final int VEH_SHIP = 0;  //订单发运主VO
	private static final int VEH_SHIP_DETAIL = 1;  //订单发运详细VO
	public static final DeVehicleShippingDao getInstance() {
		return dao;
	}
	/**
	 * 
	* @Title: getDeVehicleShipping 
	* @Description: TODO(查询订单发运接口表未下发的数据) 
	* @param @return    设定文件 
	* @return List<VehicleShippingVO>    返回类型 
	* @throws
	 */
	public List<VehicleShippingVO> getDeVehicleShipping() {
		StringBuffer sql= new StringBuffer();
		sql.append("SELECT DISTINCT(C.DELIVERY_NO), C.DELIVERY_DATE, C.DELIVERY_TYPE, C.RECEIVER, \n");
		sql.append("       D.ORDER_NO, E.ADDRESS, F.DEALER_CODE \n");
		//sql.append("FROM TT_VS_DLVRY_RPC A, TM_VEHICLE B, TT_VS_DLVRY C, TT_VS_ORDER D,\n");
		sql.append("FROM TT_VS_DLVRY_RPC A, TT_VS_DLVRY C, TT_VS_ORDER D,\n");
		sql.append("     TM_VS_ADDRESS E, TM_DEALER F \n");
		sql.append("WHERE A.IF_STATUS = ").append(DEConstant.IF_STATUS_0).append("\n");
		//sql.append("  AND A.VIN = B.VIN \n");
		sql.append("  AND A.DELIVERY_NO = C.DELIVERY_NO \n");
		sql.append("  AND C.ORDER_ID = D.ORDER_ID \n");
		sql.append("  AND C.ADDRESS_ID = E.ID \n");
		sql.append("  AND C.RECEIVER = F.DEALER_ID");
		List<VehicleShippingVO> list = select(sql.toString(), null, VEH_SHIP);
		return list;
	}
	
	/**
	 * 
	* @Title: getVehicleList 
	* @Description: TODO(货运单号去接口表查询VIN 详细信息) 
	* @param @param deNo 货运单号
	* @param @return    设定文件 
	* @return List<BaseVO>    返回类型 
	* @throws
	 */
	public List<BaseVO> getVehicleList(String deNo) {
		StringBuffer sql= new StringBuffer();
		sql.append("SELECT A.VIN, A.ENGINE_NO, A.FACTORY_DATE, A.PRODUCT_DATE,\n" );
		sql.append("       B.MATERIAL_CODE, B.VHCL_PRICE, D.CODE_ID, D.CODE_DESC\n" );
		sql.append(" FROM TM_VEHICLE A, TM_VHCL_MATERIAL B, TT_VS_DLVRY_RPC C, TC_CODE D\n" );
		sql.append("WHERE C.DELIVERY_NO = '").append(deNo).append("'");
		sql.append("  AND A.VIN = C.VIN\n");
		sql.append("  AND A.MATERIAL_ID = B.MATERIAL_ID\n");
		sql.append("  AND A.YIELDLY = D.CODE_ID\n");
		List<BaseVO> list = select(sql.toString(), null, VEH_SHIP_DETAIL);
		return list;
	}

	protected BaseVO wrapperVO(ResultSet rs, int idx, int flag) {
		switch(flag) {
		case VEH_SHIP : 
			return wrapperVehicleShippingVO(rs, idx);
		case VEH_SHIP_DETAIL : 
			return wrapperVehicleShippingDetailVO(rs, idx);
		}
		return null;
	}
	
	private VehicleShippingVO wrapperVehicleShippingVO(ResultSet rs, int idx) {
		VehicleShippingVO vo = new VehicleShippingVO();
		try {
			vo.setShippingOrderNo(rs.getString("DELIVERY_NO"));
			vo.setPoNo(rs.getString("ORDER_NO"));
			vo.setShippingDate(rs.getDate("DELIVERY_DATE"));
			vo.setDeliveryType(rs.getString("DELIVERY_TYPE"));
			vo.setShippingAddress(rs.getString("ADDRESS"));
			vo.setEntityCode(rs.getString("DEALER_CODE"));
			vo.setDownTimestamp(downTimestamp);
			vo.setIsValid(Constant.STATUS_ENABLE);
			vo.setVehicleVoList(DEUtil.transType(getVehicleList(rs.getString("DELIVERY_NO")))); 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RpcException(e.getMessage());
		}
		return vo;
	}
	
	private VehicleShippingDetailVO wrapperVehicleShippingDetailVO(ResultSet rs, int idx) {
		VehicleShippingDetailVO vo = new VehicleShippingDetailVO();
		try {
			vo.setVin(rs.getString("VIN"));
			vo.setProductCode(rs.getString("MATERIAL_CODE"));
			vo.setEngineNo(rs.getString("ENGINE_NO"));
			vo.setManufactureDate(rs.getDate("PRODUCT_DATE"));
			vo.setFactoryDate(rs.getDate("FACTORY_DATE"));
			vo.setVehiclePrice(rs.getDouble("VHCL_PRICE"));
			vo.setProductingAreaCode(String.valueOf(rs.getInt("CODE_ID")));
			vo.setProductingAreaName(rs.getString("CODE_DESC"));
		} catch (SQLException e) {
			throw new RpcException(e.getMessage());
		}
		return vo;
	}
	
}
